//
//  DMDetailViewController.m
//  CQKuaiYi
//
//  Created by 何典钊 on 2017/1/9.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMDetailViewController.h"
#import "DMIntroduction.h"
#import "DMDiagnosticV.h"
#import "DMNetworkTool.h"

@interface DMDetailViewController () <UIScrollViewDelegate>

//头像
@property (nonatomic, strong) IBOutlet UIImageView *dmIcon;
//人物名
@property (nonatomic, strong) IBOutlet UILabel *dmName;
//职业
@property (nonatomic, strong) IBOutlet UILabel *dmProfessional;
//医院
@property (nonatomic, strong) IBOutlet UILabel *dmHospital;
//点赞
@property (nonatomic, strong) IBOutlet UILabel *dmCount;
//鲜花
@property (nonatomic, strong) IBOutlet UILabel *dmFlowers;
//收藏
@property (nonatomic, strong) IBOutlet UILabel *dmCollection;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *offset_x;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet DMDiagnosticV *doctorDetail;
@property (weak, nonatomic) IBOutlet DMIntroduction *introduction;

@end

@implementation DMDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadData];
    
    [self loadDatas];
}

//加载数据
- (void) loadData{
    NSString *url = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    NSDictionary *parameters = @{@"doctor_id":@300000315};
    
    [[DMNetworkTool shareDMNetwork] postURL:url parameter:parameters completion:^(id response) {
        _introduction.introduction = response[@"data"][@"introduction"];
    }];
}
//加载接诊条件
- (void) loadDatas{
    NSString *url = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    NSDictionary *parameters = @{@"doctor_id":@300000315};
    [[DMNetworkTool shareDMNetwork] postURL:url parameter:parameters completion:^(id response) {
        _doctorDetail.receiving_setting_extra = response[@"data"][@"receiving_setting_extra"];
        _doctorDetail.receiving_settings = response[@"data"][@"receiving_settings"];
        
    }];
}

- (void) setupUI {
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    //右上角的关注按钮
    UIButton *xinButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [xinButton setImage:[UIImage imageNamed:@"collection_off"] forState:UIControlStateNormal];
    [xinButton setImage:[UIImage imageNamed:@"collection_on"] forState:UIControlStateSelected];
    [xinButton addTarget:self action:@selector(guangZhu:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:xinButton];
    self.navigationItem.rightBarButtonItem = barButton;
}

//关注是否成功
- (void)guangZhu:(UIButton *) sender{
    if (sender.selected == YES) {
        sender.selected = NO;
        [DMTools dmShowSVErrorTitle:@"取消关注" andTime:1.0];
    } else {
        sender.selected = YES;
        [DMTools dmShowSVSuccessTitle:@"关注成功" andTime:1.0];
    }
}
//赋值
- (void)setModel:(DMDoctorModel *)model {
    _model = model;
    _dmCollection.text = model.banner;
    _dmFlowers.text = model.flower;
    _dmCount.text = model.operation_count;
    _dmName.text = model.doctor_name;
    _dmProfessional.text = model.doctor_title_name;
    self.dmHospital.text = model.hospital_name;
    
    NSURL *url = [NSURL URLWithString:model.doctor_portrait];
    [self.dmIcon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
}
//滚动view
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset = scrollView.contentOffset.x;
    _offset_x.constant = offset/2;
}

//跳转接诊条件
- (IBAction)jumpacctrs:(id)sender {
    [_scrollView scrollRectToVisible:_doctorDetail.frame animated:YES];
}
//跳转简介
- (IBAction)jumpIndruiters:(id)sender {
    
    [_scrollView scrollRectToVisible:_introduction.frame animated:YES];
}


@end
